import { instance } from '../../utils/server.js';
import { filesUrl } from '../apiUrl/file';
/**
 * 检查文件是否存在
 * @param {*} params
 * @returns
 */
export const checkFileExist = (params) => {
  return new Promise((resolve, reject) => {
    instance
      .post(filesUrl.checkFileExist, params)
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};

/**
 * 文件下载
 * @param {*} params
 * @returns
 */
export const download = (params) => {
  return new Promise((resolve, reject) => {
    instance
      .post(filesUrl.download, params)
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};

/**
 * 获取指定目录下的所有文件
 * @param {*} params
 * @returns
 */
export const getBucketDrectoryAllFile = (params) => {
  return new Promise((resolve, reject) => {
    instance
      .post(filesUrl.getBucketDrectoryAllFile, params)
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};

/**
 * 文件上传
 * @param {*} params
 * @returns
 */
export const upload = (params) => {
  return new Promise((resolve, reject) => {
    instance
      .post(filesUrl.upload, params, {
        headers: {
          'Content-Type': 'multipart/form-data',
        },
      })
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};

/**
 * gerber dom
 *  文件上传
 * @param {*} params
 * @returns
 */
export const uploadDocFile = (params) => {
  return new Promise((resolve, reject) => {
    instance
      .post('/mall-api/biz/crmCustomerFile/uploadFile', params, {
        headers: {
          'Content-Type': 'multipart/form-data',
        },
      })
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};

/**
 * 图片上传
 * @param {*} params
 * @returns
 */
export const uploadImage = (params) => {
  return new Promise((resolve, reject) => {
    instance
      .post(filesUrl.uploadImage, params)
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};

/**
 * 下载bom模板
 * @param {*} params
 * @returns
 */
export const getMallFileDownLoadPath = (params) => {
  return new Promise((resolve, reject) => {
    instance
      .post(filesUrl.getMallFileDownLoadPath, params)
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};

export const standardDocumentsPath = (params) => {
  return new Promise((resolve, reject) => {
    instance
      .post(filesUrl.standardDocuments, params)
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};

/***
 * 获取文件下载路径数据
 */
export const getMallFileData = (params) => {
  return new Promise((resolve, reject) => {
    instance
      .get(`${import.meta.env.VITE_APP_FILE_DOWNLOAD_API}/${params}`, {
        timeout: 60000
      })
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};
